package com.testnacos.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import com.testnacos.service.ChacheImpl;
import com.testnacos.service.KillSunlei;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class CosumerController {

    @Autowired
    private KillSunlei killSunlei;
    @Autowired
    private ChacheImpl chache;

    @RequestMapping("killyouthen")
    public String kill(){
       return killSunlei.kill();
    }
    //模拟熔断降级
    @RequestMapping("killyouHystrix")
    @HystrixCommand(fallbackMethod = "killyouHystrixFallBack",//方法也
    commandProperties = {//属性也
            @HystrixProperty(//此乃超时时间也
                    name="execution.isolation.thread.timeoutInMilliseconds",value = "1000")})
    public String killyouHystrix() throws InterruptedException {
       Thread.sleep(3000);
        return "killyouHystrix";
    }

    public String killyouHystrixFallBack(){
        return "killyouHystrixFallBack";
    }

    @RequestMapping("killyouHystrixseeChache")
    public String killyouHystrixseeChache(){
        chache.killyouHystrixseeChache();
        return "成功";
    }



    @RequestMapping("killyouHystrixtryCollapser")
    public String killyouHystrixtryCollapser(){

        new Thread(){
            @Override
            public  void  run(){
                chache.killyouHystrixtryCollapser("孙磊");
            }
        }.start();
        new Thread(){
            @Override
            public  void  run(){
                chache.killyouHystrixtryCollapser("金烧毁");
            }
        }.start();
        new Thread(){
            @Override
            public  void  run(){
                chache.killyouHystrixtryCollapser("白月明");
            }
        }.start();
        new Thread(){
            @Override
            public  void  run(){
                chache.killyouHystrixtryCollapser("催的件");
            }
        }.start();
        return "成功";
    }


}
